﻿Imports TMySQL
Public Class frmMain
    Private Shared _Conn As New TMySession
    Private Shared _DatabaseList As TMyDatabaseList
    Private Shared _ProfileList As TMyProfileList
    Private Shared _EditorSettings As New TEditorSettings
    Private Shared _GeneralSettings As New TGeneralSettings
    Private Shared _GridSettings As New TGridSettings
    Private Shared _CaptureProcessList As TCaptureMyProcessList
    Private Const HELP_FILE_NAME As String = "Object Prowser MySQL.chm"
    Private Shared _Attributes As TMyAttributes
    Private Shared _Collations As TMyCollations
    Private Shared _CharacterSets As TMyCharacterSets
    Private Shared _ColumnTypes As TMyColumnTypes
    Private Shared _Engines As TMyEngines
    Private Shared _UserList As TMyUserList
    Private Enum TImageIndex As Integer
        HostIndex = 0
        DatabaseIndex = 1
        TableIndex = 2
        SystemViewIndex = 3
        ViewIndex = 4
        ProcedureIndex = 5
        FunctionIndex = 6
        TriggerIndex = 7
        SQLIndex = 8
        ServerInfoIndex = 9
        InnoDBStatusIndex = 10
        ProcessListIndex = 11
        ProfileIndex = 12
        SQLCaptureIndex = 13
        EventIndex = 14
    End Enum
    Public ReadOnly Property Engines() As TMyEngines
        Get
            Return _Engines
        End Get
    End Property
    Public ReadOnly Property DatabaseList() As TMyDatabaseList
        Get
            Return _DatabaseList
        End Get
    End Property
    Public ReadOnly Property Attributes() As TMyAttributes
        Get
            Return _Attributes
        End Get
    End Property
    Public ReadOnly Property Collations() As TMyCollations
        Get
            Return _Collations
        End Get
    End Property
    Public ReadOnly Property CharacterSets() As TMyCharacterSets
        Get
            Return _CharacterSets
        End Get
    End Property
    Public ReadOnly Property ColumnTypes() As TMyColumnTypes
        Get
            Return _ColumnTypes
        End Get
    End Property
    Private ReadOnly Property HelpFileName() As String
        Get
            Return Common.ApplicationPath & HELP_FILE_NAME
        End Get
    End Property

    Public ReadOnly Property GridSettings() As TGridSettings
        Get
            Return _GridSettings
        End Get
    End Property

    Public ReadOnly Property GeneralSettings() As TGeneralSettings
        Get
            Return _GeneralSettings
        End Get
    End Property
    Public ReadOnly Property EditorSettings() As TEditorSettings
        Get
            Return _EditorSettings
        End Get
    End Property

    Public Sub New()
        ' この呼び出しは、Windows フォーム デザイナで必要です。
        InitializeComponent()
        ' InitializeComponent() 呼び出しの後で初期化を追加します。
    End Sub

    Private Sub InitFrm()
        StatusBar.Text = _Conn.UserName & "@" & _Conn.HostName
        InitGeneralSettings()
        InitTree()
    End Sub

    Public ReadOnly Property CaptureProcessList() As TCaptureMyProcessList
        Get
            Return _CaptureProcessList
        End Get
    End Property

    Private Sub InitGeneralSettings()
        Me.Left = _GeneralSettings.LeftPos
        Me.Top = _GeneralSettings.TopPos
        Me.Width = _GeneralSettings.Width
        Me.Height = _GeneralSettings.Height
        If _GeneralSettings.Maximized Then
            Me.WindowState = FormWindowState.Maximized
        End If
        Me.Font = _GeneralSettings.Font
    End Sub
    Public ReadOnly Property ProfileList() As TMyProfileList
        Get
            Return _ProfileList
        End Get
    End Property

    Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = MsgBox("終了します。よろしいですか？", MsgBoxStyle.OkCancel) <> MsgBoxResult.Ok
    End Sub

    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        RemoveHandler Me.FormClosing, AddressOf frmMain_FormClosing
        Dim frmRet As New frmLogin(_Conn)
        Try
            _EditorSettings = New TEditorSettings
            _EditorSettings.Load()
            frmRet.Activate()
            If Not frmRet.ShowEx Then
                Me.Close()
                Return
            End If
            AddHandler Me.FormClosing, AddressOf frmMain_FormClosing
            _DatabaseList = New TMyDatabaseList(_Conn)
            _ProfileList = New TMyProfileList(_Conn)
            _CaptureProcessList = New TCaptureMyProcessList(_Conn)
            _Collations = New TMyCollations(_Conn)
            _CharacterSets = New TMyCharacterSets(_Conn)
            _ColumnTypes = New TMyColumnTypes
            _Attributes = New TMyAttributes
            _Engines = New TMyEngines(_Conn)
            _UserList = New TMyUserList(_Conn)
            InitFrm()
        Finally
            frmRet.Dispose()
        End Try
    End Sub

    Public ReadOnly Property Connection() As TMySession
        Get
            Return _Conn
        End Get
    End Property

    Private Sub InitTree()
        Dim nodeD As TreeNode
        Dim node As TreeNode
        Dim nodeP As New TreeNode(_DatabaseList.HostName, TImageIndex.HostIndex, TImageIndex.HostIndex)
        tv.Nodes.Add(nodeP)
        For i As Integer = 0 To _DatabaseList.Count - 1
            nodeD = New TreeNode(_DatabaseList.Database(i).DatabaseName, TImageIndex.DatabaseIndex, TImageIndex.DatabaseIndex)
            nodeP.Nodes.Add(nodeD)

            node = New TreeNode("テーブル", TImageIndex.TableIndex, TImageIndex.TableIndex)
            node.Tag = _DatabaseList.Database(i).Tables
            nodeD.Nodes.Add(node)

            node = New TreeNode("システムビュー", TImageIndex.SystemViewIndex, TImageIndex.SystemViewIndex)
            node.Tag = _DatabaseList.Database(i).SystemViews
            nodeD.Nodes.Add(node)

            node = New TreeNode("ビュー", TImageIndex.ViewIndex, TImageIndex.ViewIndex)
            node.Tag = _DatabaseList.Database(i).Views
            nodeD.Nodes.Add(node)

            node = New TreeNode("プロシージャ", TImageIndex.ProcedureIndex, TImageIndex.ProcedureIndex)
            node.Tag = _DatabaseList.Database(i).Procedures
            nodeD.Nodes.Add(node)

            node = New TreeNode("ファンクション", TImageIndex.FunctionIndex, TImageIndex.FunctionIndex)
            node.Tag = _DatabaseList.Database(i).Functions
            nodeD.Nodes.Add(node)

            node = New TreeNode("トリガー", TImageIndex.TriggerIndex, TImageIndex.TriggerIndex)
            node.Tag = _DatabaseList.Database(i).Triggers
            nodeD.Nodes.Add(node)

            node = New TreeNode("イベント", TImageIndex.EventIndex, TImageIndex.EventIndex)
            node.Tag = _DatabaseList.Database(i).Events
            nodeD.Nodes.Add(node)

            If _Conn.DatabaseName = _DatabaseList.Database(i).DatabaseName Then
                nodeD.Expand()
            End If
        Next
        nodeP.Expand()
    End Sub

    Private Sub tv_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tv.AfterSelect
        Cursor.Current = Cursors.WaitCursor
        lv.BeginUpdate()
        Try
            ClearObjects()
            If e.Node.Level = 2 Then
                Dim obj As TCustomMyObjects = DirectCast(e.Node.Tag, TCustomMyObjects)
                obj.Refresh()
                ShowObjects(obj)
                For Each col As ColumnHeader In lv.Columns
                    col.Width = -2
                Next
            End If
        Finally
            lv.EndUpdate()
            Cursor.Current = Cursors.Default
        End Try
    End Sub

    Private Sub ShowObjects(ByVal AObjects As TCustomMyObjects)
        Select Case AObjects.GetType.ToString
            Case "TMySQL.TMyTables"
                ShowTables(AObjects)
            Case "TMySQL.TMyViews"
                ShowViews(AObjects)
            Case "TMySQL.TMySystemViews"
                ShowSystemViews(AObjects)
            Case "TMySQL.TMyProcedures"
                ShowProcedures(AObjects)
            Case "TMySQL.TMyFunctions"
                ShowFunctions(AObjects)
            Case "TMySQL.TMyTriggers"
                ShowTriggers(AObjects)
            Case "TMySQL.TMyEvents"
                ShowEvents(AObjects)
            Case Else
                Call MsgBox("指定されたオブジェクトはサポートされていません", MsgBoxStyle.Exclamation)
        End Select
    End Sub

    Private Sub ClearObjects()
        lv.Columns.Clear()
        lv.Items.Clear()
    End Sub

    Private Sub ShowTableHeaders()
        lv.Columns.Clear()
        lv.Columns.Add("名前")
        lv.Columns.Add("エンジン")
        lv.Columns.Add("作成日時")
        lv.Columns.Add("更新日時")
        lv.Columns.Add("コメント")
    End Sub

    Private Sub ShowTables(ByVal ATables As TMyTables)
        ClearObjects()
        ShowTableHeaders()
        For i As Integer = 0 To ATables.Count - 1
            Dim tab As TMyTable = ATables(i)
            Dim item As New ListViewItem(tab.TableName)
            item.SubItems.Add(tab.Engine)
            item.SubItems.Add(Common.DateTimeToString(tab.CreateTime))
            item.SubItems.Add(Common.DateTimeToString(tab.UpdateTime))
            item.SubItems.Add(tab.TableComment)
            lv.Items.Add(item)
        Next
    End Sub

    Private Sub ShowViewHeaders()

        lv.Columns.Clear()
        lv.Columns.Add("名前")
        lv.Columns.Add("スキーマ")
        lv.Columns.Add("作成者")
        lv.Columns.Add("チェックオプション")
        lv.Columns.Add("更新可能")
    End Sub

    Private Sub ShowViews(ByVal AViews As TMyViews)
        ClearObjects()
        ShowViewHeaders()
        For i As Integer = 0 To AViews.Count - 1
            Dim view As TMyView = AViews(i)
            Dim item As New ListViewItem(view.TableName)
            item.SubItems.Add(view.TableSchema)
            item.SubItems.Add(view.Definer)
            item.SubItems.Add(view.CheckOption)
            item.SubItems.Add(view.IsUpdatable)
            lv.Items.Add(item)
        Next
    End Sub

    Private Sub ShowSystemViewHeaders()
        lv.Columns.Clear()
        lv.Columns.Add("名前")
        lv.Columns.Add("エンジン")
        lv.Columns.Add("作成日時")
        lv.Columns.Add("更新日時")
        lv.Columns.Add("コメント")
    End Sub

    Private Sub ShowSystemViews(ByVal ASystemViews As TMySystemViews)
        ClearObjects()
        ShowSystemViewHeaders()
        For i As Integer = 0 To ASystemViews.Count - 1
            Dim sview As TMySystemView = ASystemViews(i)
            Dim item As New ListViewItem(sview.TableName)
            item.SubItems.Add(sview.Engine)
            item.SubItems.Add(Common.DateTimeToString(sview.CreateTime))
            item.SubItems.Add(Common.DateTimeToString(sview.UpdateTime))
            item.SubItems.Add(sview.TableComment)
            lv.Items.Add(item)
        Next
    End Sub

    Private Sub ShowProcedureHeaders()
        lv.Columns.Clear()
        lv.Columns.Add("名前")
        lv.Columns.Add("スキーマ")
        lv.Columns.Add("作成者")
        lv.Columns.Add("作成日時")
        lv.Columns.Add("更新日時")
        lv.Columns.Add("コメント")
    End Sub

    Private Sub ShowProcedures(ByVal AProcedures As TMyProcedures)
        ClearObjects()
        ShowProcedureHeaders()
        For i As Integer = 0 To AProcedures.Count - 1
            Dim proc As TMyProcedure = AProcedures(i)
            Dim item As New ListViewItem(proc.RoutineName)
            item.SubItems.Add(proc.RoutineSchema)
            item.SubItems.Add(proc.Definer)
            item.SubItems.Add(Common.DateTimeToString(proc.Created))
            item.SubItems.Add(Common.DateTimeToString(proc.LastAltered))
            item.SubItems.Add(proc.RoutineComment)
            lv.Items.Add(item)
        Next
    End Sub

    Private Sub ShowFunctionHeaders()
        lv.Columns.Clear()
        lv.Columns.Add("名前")
        lv.Columns.Add("スキーマ")
        lv.Columns.Add("作成者")
        lv.Columns.Add("作成日時")
        lv.Columns.Add("更新日時")
        lv.Columns.Add("コメント")
    End Sub


    Private Sub ShowFunctions(ByVal AFunctions As TMyFunctions)
        ClearObjects()
        ShowFunctionHeaders()
        For i As Integer = 0 To AFunctions.Count - 1
            Dim func As TMyFunction = AFunctions(i)
            Dim item As New ListViewItem(func.RoutineName)
            item.SubItems.Add(func.RoutineSchema)
            item.SubItems.Add(func.Definer)
            item.SubItems.Add(Common.DateTimeToString(func.Created))
            item.SubItems.Add(Common.DateTimeToString(func.LastAltered))
            item.SubItems.Add(func.RoutineComment)
            lv.Items.Add(item)
        Next
    End Sub

    Private Sub ShowTriggerHeaders()
        lv.Columns.Clear()
        lv.Columns.Add("名前")
        lv.Columns.Add("作成者")
        lv.Columns.Add("タイミング")
        lv.Columns.Add("対象スキーマ")
        lv.Columns.Add("対象テーブル")
    End Sub

    Private Sub ShowTriggers(ByVal ATriggers As TMyTriggers)
        ClearObjects()
        ShowTriggerHeaders()
        For i As Integer = 0 To ATriggers.Count - 1
            Dim trig As TMyTrigger = ATriggers(i)
            Dim item As New ListViewItem(trig.TriggerName)
            item.SubItems.Add(trig.Definer)
            item.SubItems.Add(trig.ActionTiming & " " & trig.EventManipulation)
            item.SubItems.Add(trig.EventObjectSchema)
            item.SubItems.Add(trig.EventObjectTable)
            lv.Items.Add(item)
        Next
    End Sub

    Private Sub ShowEventHeaders()
        lv.Columns.Clear()
        lv.Columns.Add("名前")
        lv.Columns.Add("期間")
        lv.Columns.Add("スパン/実行日時")
        lv.Columns.Add("最終実行日時")
        lv.Columns.Add("作成日時")
        lv.Columns.Add("更新日時")
        lv.Columns.Add("コメント")
    End Sub

    Private Sub ShowEvents(ByVal AEvents As TMyEvents)
        ClearObjects()
        ShowEventHeaders()
        For i As Integer = 0 To AEvents.Count - 1
            Dim ev As TMyEvent = AEvents(i)
            Dim item As New ListViewItem(ev.EventName)
            If ev.Starts.Equals(DateTime.MinValue) AndAlso ev.Ends.Equals(DateTime.MinValue) Then
                item.SubItems.Add(String.Empty)
            Else
                item.SubItems.Add(Common.DateTimeToString(ev.Starts) & "～" & Common.DateTimeToString(ev.Ends))
            End If

            If ev.EventType = "ONE TIME" Then
                item.SubItems.Add(Common.DateTimeToString(ev.ExecuteAt))
            Else
                item.SubItems.Add(ev.IntervalValue.ToString & " " & ev.IntervalField)
            End If
            item.SubItems.Add(Common.DateTimeToString(ev.LastExecuted))
            item.SubItems.Add(Common.DateTimeToString(ev.LastAltered))
            item.SubItems.Add(Common.DateTimeToString(ev.LastAltered))

            item.SubItems.Add(ev.EventComment)
            lv.Items.Add(item)
        Next
    End Sub

    Private Sub cmdSQL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSQL.Click, SQL実行SToolStripMenuItem.Click
        Dim frmRet As New frmSQL
        AddTabSheet(frmRet, TImageIndex.SQLIndex, "SQL")
    End Sub

    Private Sub AddTabSheet(ByVal AForm As Form, ByVal AImageIndex As TImageIndex, ByVal ACaption As String)
        Dim tbs As New TabPage
        AForm.TopLevel = False
        tbs.Text = ACaption
        tbs.ImageIndex = AImageIndex
        AForm.Parent = tbs
        AForm.Dock = DockStyle.Fill
        pgc.TabPages.Add(tbs)
        pgc.SelectedIndex = pgc.TabPages.Count - 1
        AForm.Show()
        Me.Refresh()
    End Sub

    Private Function ActivateTabSheet(ByVal AType As System.Type) As Integer
        For i As Integer = 0 To pgc.TabPages.Count - 1
            If pgc.TabPages(i).Controls(0).GetType Is AType Then
                pgc.SelectedTab = pgc.TabPages(i)
                Return i
            End If
        Next
        Return -1
    End Function

    Private Sub cmdProcessList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdProcessList.Click, プロセスリストLToolStripMenuItem.Click
        If ActivateTabSheet(GetType(frmProcessList)) < 0 Then
            AddTabSheet(New frmProcessList, TImageIndex.ProcessListIndex, "プロセスリスト")
        End If
    End Sub

    Private Sub cmdProfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdProfile.Click, プロファイルPToolStripMenuItem.Click
        If ActivateTabSheet(GetType(frmProfile)) < 0 Then
            AddTabSheet(New frmProfile, TImageIndex.ProfileIndex, "プロファイル")
        End If
    End Sub

    Private Sub cmdServerInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdServerInfo.Click, サーバー情報IToolStripMenuItem.Click
        If ActivateTabSheet(GetType(frmServerInfo)) < 0 Then
            AddTabSheet(New frmServerInfo, TImageIndex.ServerInfoIndex, "サーバー情報")
        End If
    End Sub

    Private Sub cmdPrivs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrivs.Click
        If ActivateTabSheet(GetType(frmPrivileges)) < 0 Then
            AddTabSheet(New frmPrivileges(_UserList), TImageIndex.SQLCaptureIndex, "権限")
        End If
    End Sub

    Private Sub cmdSQLCapture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSQLCapture.Click, SQL監視CToolStripMenuItem.Click
        If ActivateTabSheet(GetType(frmSQLCapture)) < 0 Then
            AddTabSheet(New frmSQLCapture, TImageIndex.SQLCaptureIndex, "SQL監視")
        End If
    End Sub

    Private Sub cmdInnoDBStatus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdInnoDBStatus.Click, InnoDBStatusIToolStripMenuItem.Click
        If ActivateTabSheet(GetType(frmInnoDBStatus)) < 0 Then
            AddTabSheet(New frmInnoDBStatus, TImageIndex.InnoDBStatusIndex, "InnoDB Status")
        End If
    End Sub

    Private Sub mnuProfile_CheckStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuProfile.CheckStateChanged
        _ProfileList.Enabled = mnuProfile.Checked
        If mnuProfile.Checked Then
            StatusBarProfile.Text = "プロファイル取得中"
        Else
            StatusBarProfile.Text = String.Empty
        End If
    End Sub

    Private Sub lv_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lv.DoubleClick
        If lv.SelectedItems.Count <= 0 Then
            Return
        End If
        ShowObject(DirectCast(tv.SelectedNode.Tag, TCustomMyObjects)(lv.SelectedItems(0).Index))
    End Sub

    Private Sub ShowObject(ByVal AObject As TCustomMyObject)
        Select Case AObject.GetType.ToString
            Case "TMySQL.TMyTable"
                ShowTable(AObject)
            Case "TMySQL.TMyView"
                ShowView(AObject)
            Case "TMySQL.TMySystemView"
                ShowSystemView(AObject)
            Case "TMySQL.TMyProcedure"
                ShowProcedure(AObject)
            Case "TMySQL.TMyFunction"
                ShowFunction(AObject)
            Case "TMySQL.TMyTrigger"
                ShowTrigger(AObject)
            Case "TMySQL.TMyEvent"
                ShowEvent(AObject)
            Case Else
                Call MsgBox("指定されたオブジェクトはサポートされていません", MsgBoxStyle.Exclamation)
        End Select
    End Sub

    Private Sub ShowTable(ByVal ATable As TMyTable)
        AddTabSheet(New frmTable(ATable), TImageIndex.TableIndex, ATable.DatabaseName & "." & ATable.TableName)
    End Sub

    Private Sub ShowView(ByVal AView As TMyView)
        AddTabSheet(New frmTable(AView), TImageIndex.ViewIndex, AView.DatabaseName & "." & AView.TableName)
    End Sub

    Private Sub ShowSystemView(ByVal ASystemView As TMySystemView)
        AddTabSheet(New frmTable(ASystemView), TImageIndex.SystemViewIndex, ASystemView.DatabaseName & "." & ASystemView.TableName)
    End Sub

    Private Sub ShowProcedure(ByVal AProcedure As TMyProcedure)
        AddTabSheet(New frmProcedure(AProcedure), TImageIndex.ProcedureIndex, AProcedure.DatabaseName & "." & AProcedure.RoutineName)
    End Sub

    Private Sub ShowFunction(ByVal AFunction As TMyFunction)
        AddTabSheet(New frmProcedure(AFunction), TImageIndex.FunctionIndex, AFunction.DatabaseName & "." & AFunction.RoutineName)
    End Sub

    Private Sub ShowTrigger(ByVal ATrigger As TMyTrigger)
        AddTabSheet(New frmTrigger(ATrigger), TImageIndex.TriggerIndex, ATrigger.DatabaseName & "." & ATrigger.TriggerName)
    End Sub

    Private Sub ShowEvent(ByVal AEvent As TMyEvent)
        AddTabSheet(New frmEvent(AEvent), TImageIndex.EventIndex, AEvent.DatabaseName & "." & AEvent.EventName)
    End Sub

    Private Sub mnuEditorFont_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEditorFont.Click
        Dim frmRet As New frmFont
        Try
            If frmRet.ShowEx = Windows.Forms.DialogResult.OK Then
                Cursor.Current = Cursors.WaitCursor
                Try
                    _EditorSettings.ApplyAll(Me)
                Finally
                    Cursor.Current = Cursors.Default
                End Try
            End If
        Finally
            frmRet.Dispose()
        End Try
    End Sub

    Private Sub frmMain_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        SaveGeneralSettings()
    End Sub

    Private Sub SaveGeneralSettings()
        _GeneralSettings.Height = Me.Height
        _GeneralSettings.Width = Me.Width
        _GeneralSettings.TopPos = Me.Top
        _GeneralSettings.LeftPos = Me.Left
        _GeneralSettings.Maximized = Me.WindowState = FormWindowState.Maximized
        _GeneralSettings.SaveConfig()
    End Sub

    Private Sub mnuGrid_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuGrid.Click
        Dim frmRet As New frmGridColor
        Try
            If frmRet.ShowEx = Windows.Forms.DialogResult.OK Then
                Cursor.Current = Cursors.WaitCursor
                Try
                    _GridSettings.ApplyAll(Me)
                Finally
                    Cursor.Current = Cursors.Default
                End Try
            End If
        Finally

        End Try
    End Sub

    Private Sub bgworker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgworker.DoWork
        Static i As Integer = 0
        While True
            Try
                If bgworker.CancellationPending Then
                    e.Cancel = True
                    Return
                End If
                _CaptureProcessList.ExecuteCapture()
            Catch ex As Exception
            End Try
            System.Threading.Thread.Sleep(_CaptureProcessList.CaptureSpan)
        End While
    End Sub

    Private Sub StartCapture()
        _CaptureProcessList.StartCapture()
        bgworker.RunWorkerAsync()
        While Not bgworker.IsBusy
            Application.DoEvents()
        End While
        StatusBarCapture.Text = "SQL監視中"
    End Sub

    Private Sub StopCapture()
        bgworker.CancelAsync()
        _CaptureProcessList.StopCapture()
        While bgworker.IsBusy
            Application.DoEvents()
        End While
        StatusBarCapture.Text = String.Empty
    End Sub

    Private Sub mnuSQLCapture_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSetSQLCapture.CheckedChanged
        If mnuSetSQLCapture.Checked Then
            StartCapture()
        Else
            StopCapture()
        End If
    End Sub

    Private Sub mnuSQLCaptureConfig_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSQLCaptureConfig.Click
        Dim frmRet As New frmSQLCaptureConfig
        Try
            frmRet.ShowEx(_CaptureProcessList)
        Finally
            frmRet.Dispose()
        End Try
    End Sub

    Private Sub mnuQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuQuit.Click
        Me.Close()
    End Sub

    Private Sub mnuVersion_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuVersion.Click
        frmVersion.ShowDialog()
    End Sub

    Private Sub ObjectProwserMySQLのヘルプHToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ObjectProwserMySQLのヘルプHToolStripMenuItem.Click
        Help.ShowHelp(Me, Me.HelpFileName, HelpNavigator.Topic)
    End Sub

    Protected Overrides Sub Finalize()
        If bgworker.IsBusy Then
            bgworker.CancelAsync()
        End If
        If _DatabaseList IsNot Nothing Then
            _DatabaseList.Dispose()
        End If
        If _ProfileList IsNot Nothing Then
            _ProfileList.Dispose()
        End If
        If _CaptureProcessList IsNot Nothing Then
            _CaptureProcessList.Dispose()
        End If
        If _GeneralSettings IsNot Nothing Then
            _GeneralSettings.Dispose()
        End If
        If _EditorSettings IsNot Nothing Then
            _EditorSettings.Dispose()
        End If
        If _GridSettings IsNot Nothing Then
            _GridSettings.Dispose()
        End If

        If _Collations IsNot Nothing Then
            _Collations.Dispose()
        End If
        If _CharacterSets IsNot Nothing Then
            _CharacterSets.Dispose()
        End If
        If _ColumnTypes IsNot Nothing Then
            _ColumnTypes.Dispose()
        End If
        If _Attributes IsNot Nothing Then
            _Attributes.Dispose()
        End If

        If _Conn.Connected Then
            _Conn.Connected = False
        End If
        _Conn.Dispose()
        MyBase.Finalize()
        Application.Exit()
    End Sub

    Private Sub mnuDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles popDelete.Click
        If pgc.SelectedTab IsNot tbsObject Then
            pgc.SelectedTab.Dispose()
        End If
    End Sub

    Private Sub popDeleteOther_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles popDeleteOther.Click
        Dim activeTab As TabPage = pgc.SelectedTab
        For Each tbs As TabPage In pgc.TabPages
            If tbs IsNot tbsObject AndAlso tbs IsNot activeTab Then
                tbs.Dispose()
            End If
        Next
    End Sub

    Private Sub popDeleteAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles popDeleteAll.Click
        For Each tbs As TabPage In pgc.TabPages
            If tbs IsNot tbsObject Then
                tbs.Dispose()
            End If
        Next
    End Sub

    Private Sub popupMenu_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles popupMenu.Opening
        Dim isFocused As Boolean = pgc.Focused
        For i As Integer = 0 To pgc.TabPages.Count - 1
            If pgc.TabPages(i).Focused Then
                isFocused = True
            End If
        Next
        e.Cancel = Not isFocused
        popDelete.Enabled = pgc.SelectedTab IsNot tbsObject
    End Sub

    Private Sub mnuConfig_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuConfig.Click
        Dim frmRet As New frmConfig
        Try
            If frmRet.ShowDialog = Windows.Forms.DialogResult.OK Then
                _GeneralSettings.ApplyAll(Me)
            End If
        Finally
            frmRet.Dispose()
        End Try
    End Sub

    Private Sub cmdCreateTable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCreateTable.Click
        Dim SchemaName As String = String.Empty
        If tv.SelectedNode IsNot Nothing Then
            Select Case tv.SelectedNode.Level
                Case 1
                    SchemaName = tv.SelectedNode.Text
                Case 2
                    SchemaName = tv.SelectedNode.Parent.Text
            End Select
        End If
        Dim frmRet As New frmCreateTable(SchemaName)
        Try
            frmRet.ShowDialog()
        Finally
            frmRet.Dispose()
        End Try
    End Sub
End Class
